package com.zy.studentSide.controller;

import com.zy.studentSide.entity.StudentClassroom;
import com.zy.studentSide.entity.StudentCourse;
import com.zy.studentSide.service.IStudentClassroomService;
import com.zy.studentSide.service.IStudentCourseService;
import com.zy.teacherSide.entity.Classroom;
import com.zy.teacherSide.entity.Course;
import com.zy.teacherSide.entity.Teacher;
import com.zy.teacherSide.entity.dto.StudentCourseDTO;
import com.zy.teacherSide.service.IClassroomService;
import com.zy.teacherSide.service.ICourseService;
import com.zy.teacherSide.service.ITeacherService;
import com.zy.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 29787
 */
@RestController
public class StudentCourseController {

    @Autowired
    private ICourseService iCourseService;

    @Autowired
    private IStudentClassroomService iStudentClassroomService;

    @Autowired
    private IClassroomService iClassroomService;

    @Autowired
    private IStudentCourseService iStudentCourseService;

    @Autowired
    private ITeacherService iTeacherService;

    /**
     * 学生查看当前已选班级下的所有课程信息
     * @param studentId
     * @return R
     */
    @PostMapping
    @RequestMapping(value = "/studentSeeAllCourse/{studentId}")
    public R studentSeeAllCourse(@PathVariable Integer studentId){
        List<StudentClassroom> sc_list = iStudentClassroomService.getJoinClassroomByStudentId(studentId);

        if (sc_list.size() == 0) {
            return new R(true,null);
        } else {
            List<StudentCourseDTO> cOV = new ArrayList<StudentCourseDTO>();
            for (StudentClassroom sc : sc_list){
                List<Course> course = iCourseService.getCourseByClassroomIdAndTeacherId(sc.getTeacherId(), sc.getClassroomId());
                for (Course c : course){
                    StudentCourseDTO ov = new StudentCourseDTO();
                    Classroom classroom = iClassroomService.getClassroomByClassroomId(sc.getClassroomId());
                    ov.setClassroomName(classroom.getClassroomName());
                    ov.setCourseName(c.getCourseName());
                    ov.setCredit(c.getCredit());
                    ov.setCourseId(c.getCourseId());
                    ov.setTeacherId(c.getTeacherId());
                    ov.setClassroomId(classroom.getClassroomId());
                    ov.setStudentId(studentId);
                    cOV.add(ov);
                }
            }
            return new R(true,cOV);
        }

    }

    /**
     * 学生选课
     * @param scDTO
     * @return R
     */
    @PostMapping
    @RequestMapping(value = "/studentSelectCourse/{studentId}")
    public R studentSelectCourse(@RequestBody StudentCourseDTO scDTO,@PathVariable Integer studentId){
        StudentCourse studentCourse = iStudentCourseService.getStudentCourse(studentId, scDTO.getCourseId(), scDTO.getTeacherId(), scDTO.getClassroomId());

        if (studentCourse == null){
            StudentCourse sc = new StudentCourse();
            sc.setStudentId(scDTO.getStudentId());
            sc.setCourseId(scDTO.getCourseId());
            sc.setClassroomId(scDTO.getClassroomId());
            sc.setTeacherId(scDTO.getTeacherId());
            return new R(iStudentCourseService.save(sc),"success");
        } else {
            return new R(false,"err");
        }
    }

    /**
     * 学生获取已选择的课程
     * @param studentId
     * @return R
     */
    @PostMapping
    @RequestMapping(value = "/studentSeeSelectedCourse/{studentId}")
    public R studentSeeSelectedCourse(@PathVariable Integer studentId){

        List<StudentCourse> sc_list = iStudentCourseService.getAllStudentCourse(studentId);

        if (sc_list.size() == 0){
            return new R(false,null);
        } else {
            List<StudentCourseDTO> courseOV = new ArrayList<StudentCourseDTO>();
            for (StudentCourse sc : sc_list){
                StudentCourseDTO cOV = new StudentCourseDTO();
                Course c = iCourseService.getAllCourseByCourseId(sc.getTeacherId(), sc.getCourseId());
                cOV.setCourseName(c.getCourseName());
                cOV.setCredit(c.getCredit());
                cOV.setCourseId(c.getCourseId());
                cOV.setTeacherId(sc.getTeacherId());
                Teacher t = iTeacherService.getTeacherByTeacherId(sc.getTeacherId());
                cOV.setTeacherName(t.getTeacherName());
                Classroom classroom = iClassroomService.getClassroomByClassroomId(sc.getClassroomId());
                cOV.setClassroomName(classroom.getClassroomName());
                cOV.setClassroomId(classroom.getClassroomId());
                courseOV.add(cOV);
            }
            return new R(true,courseOV);
        }

    }

    /**
     * 学生退课
     * @param scDTO
     * @return R
     */
    @PostMapping
    @RequestMapping(value = "/studentSignOutCourse/{studentId}")
    public R studentSignOutCourse(@RequestBody StudentCourseDTO scDTO,@PathVariable Integer studentId){
        StudentCourse sc = iStudentCourseService.getStudentCourse(studentId, scDTO.getCourseId(), scDTO.getTeacherId(), scDTO.getClassroomId());

        return new R(iStudentCourseService.removeById(sc.getId()));
    }

}
